<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">🔢</text>
						<text class="tool-title">数字解析</text>
						<text class="tool-subtitle">手机号、车牌号吉凶分析</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">数字信息</text>
						
						<view class="form-item">
							<text class="form-label">类型选择</text>
							<view class="type-selector">
								<view 
									v-for="type in numberTypes" 
									:key="type.value"
									class="type-item"
									:class="{ active: params.type === type.value }"
									@tap="selectType(type.value)"
								>
									<text class="type-icon">{{ type.icon }}</text>
									<text class="type-name">{{ type.name }}</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">{{ getNumberLabel() }}</text>
							<input 
								v-model="params.number" 
								:placeholder="getNumberPlaceholder()"
								class="input-field"
								:type="params.type === 'idCard' ? 'text' : 'number'"
							/>
						</view>
						
						<view class="form-item" v-if="params.type === 'license'">
							<text class="form-label">车辆信息（可选）</text>
							<view class="vehicle-info">
								<input 
									v-model="params.vehicleInfo.brand" 
									placeholder="车辆品牌"
									class="mini-input"
								/>
								<input 
									v-model="params.vehicleInfo.model" 
									placeholder="车型"
									class="mini-input"
								/>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '解析中...' : '开始解析'" 
						type="primary"
						@tap="queryNumerology"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="numerologyResult">
					<view class="result-section">
						<text class="section-title">数字解析</text>
						
						<view class="numerology-analysis">
							<view class="overall-numerology">
								<view class="numerology-score">
									<view class="score-circle">
										<text class="score-text">{{ numerologyResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ numerologyResult.overall.level }}</text>
								</view>
								<view class="numerology-desc">
									<text class="desc-text">{{ numerologyResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="number-analysis">
								<view class="analysis-item">
									<text class="analysis-label">数字号码</text>
									<text class="analysis-value">{{ numerologyResult.analysis.number }}</text>
								</view>
								<view class="analysis-item">
									<text class="analysis-label">类型</text>
									<text class="analysis-value">{{ numerologyResult.analysis.type }}</text>
								</view>
								<view class="analysis-item">
									<text class="analysis-label">主数字</text>
									<text class="analysis-value">{{ numerologyResult.analysis.MainNumber }}</text>
								</view>
								<view class="analysis-item">
									<text class="analysis-label">数字含义</text>
									<text class="analysis-meaning">{{ numerologyResult.analysis.meaning }}</text>
								</view>
							</view>
							
							<view class="lucky-analysis">
								<view class="lucky-title">吉凶分析</view>
								<view class="lucky-digits">
									<text class="digits-label">吉利数字：</text>
									<view class="digits-list">
										<text 
											v-for="digit in numerologyResult.analysis.luckyDigits" 
											:key="digit"
											class="digit-item lucky"
										>{{ digit }}</text>
									</view>
								</view>
								<view class="unlucky-digits">
									<text class="digits-label">忌用数字：</text>
									<view class="digits-list">
										<text 
											v-for="digit in numerologyResult.analysis.unluckyDigits" 
											:key="digit"
											class="digit-item unlucky"
										>{{ digit }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 详细分析 -->
				<app-card v-if="numerologyResult && numerologyResult.details">
					<view class="details-section">
						<text class="section-title">运势分析</text>
						
						<view class="details-list">
							<view 
								v-for="(detail, index) in numerologyResult.details" 
								:key="index"
								class="detail-item"
							>
								<view class="detail-header">
									<text class="detail-aspect">{{ detail.aspect }}</text>
									<view class="detail-score">
										<text class="score-text">{{ detail.score }}分</text>
									</view>
								</view>
								<text class="detail-description">{{ detail.description }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 建议 -->
				<app-card v-if="numerologyResult && numerologyResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">使用建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in numerologyResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'NumerologyToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				type: '',
				number: '',
				vehicleInfo: {
					brand: '',
					model: ''
				}
			},
			numerologyResult: null,
			loading: false,
			toolsService: null,
			numberTypes: [
				{ value: 'mobile', name: '手机号', icon: '📱' },
				{ value: 'license', name: '车牌号', icon: '🚗' },
				{ value: 'idCard', name: '身份证', icon: '🆔' },
				{ value: 'house', name: '房号', icon: '🏠' }
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.type && this.params.number.trim().length > 0
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		selectType(type) {
			this.params.type = type
			this.params.number = '' // 清空之前输入的数字
		},
		
		getNumberLabel() {
			const labels = {
				'mobile': '手机号码',
				'license': '车牌号码',
				'idCard': '身份证号',
				'house': '房屋号码'
			}
			return labels[this.params.type] || '数字号码'
		},
		
		getNumberPlaceholder() {
			const placeholders = {
				'mobile': '请输入11位手机号码',
				'license': '请输入车牌号码',
				'idCard': '请输入身份证号码',
				'house': '请输入房屋号码'
			}
			return placeholders[this.params.type] || '请输入数字号码'
		},
		
		async queryNumerology() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请选择类型并输入号码',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getNumerologyInfo(this.params)
				
				if (result.success) {
					this.numerologyResult = result.data
					uni.showToast({
						title: '解析完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '解析失败')
				}
			} catch (error) {
				console.error('数字解析失败:', error)
				uni.showToast({
					title: '解析失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.type-selector {
			display: grid;
			grid-template-columns: repeat(2, 1fr);
			gap: 16rpx;
			
			.type-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #9C27B0;
					background: linear-gradient(135deg, #9C27B0 0%, #7B1FA2 100%);
					
					.type-icon,
					.type-name {
						color: white;
					}
				}
				
				.type-icon {
					font-size: 32rpx;
					margin-bottom: 8rpx;
				}
				
				.type-name {
					font-size: 24rpx;
					color: #000000;
				}
			}
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.vehicle-info {
			display: flex;
			gap: 16rpx;
			
			.mini-input {
				flex: 1;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				font-size: 28rpx;
				background-color: #fafafa;
			}
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.numerology-analysis {
		.overall-numerology {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 32rpx;
			background: linear-gradient(135deg, #9C27B0 0%, #7B1FA2 100%);
			border-radius: 16rpx;
			
			.numerology-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.numerology-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
		
		.number-analysis {
			margin-bottom: 32rpx;
			padding: 24rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.analysis-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 16rpx;
				
				&:last-child {
					margin-bottom: 0;
					flex-direction: column;
					align-items: flex-start;
				}
				
				.analysis-label {
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.7);
				}
				
				.analysis-value {
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
				
				.analysis-meaning {
					margin-top: 8rpx;
					font-size: 26rpx;
					color: #9C27B0;
					line-height: 1.5;
				}
			}
		}
		
		.lucky-analysis {
			padding: 24rpx;
			background-color: #e8f5e8;
			border-radius: 12rpx;
			
			.lucky-title {
				font-size: 28rpx;
				font-weight: 500;
				color: #000000;
				margin-bottom: 20rpx;
			}
			
			.lucky-digits,
			.unlucky-digits {
				display: flex;
				align-items: center;
				margin-bottom: 16rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.digits-label {
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.7);
					margin-right: 16rpx;
				}
				
				.digits-list {
					display: flex;
					gap: 8rpx;
					
					.digit-item {
						padding: 8rpx 12rpx;
						border-radius: 20rpx;
						font-size: 24rpx;
						font-weight: 500;
						
						&.lucky {
							background-color: #4CAF50;
							color: white;
						}
						
						&.unlucky {
							background-color: #F44336;
							color: white;
						}
					}
				}
			}
		}
	}
}

.details-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.details-list {
		.detail-item {
			margin-bottom: 20rpx;
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.detail-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 12rpx;
				
				.detail-aspect {
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
				
				.detail-score {
					.score-text {
						font-size: 24rpx;
						color: #9C27B0;
						font-weight: 500;
					}
				}
			}
			
			.detail-description {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}

.suggestions-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}
</style>